#Include "Protheus.ch"
#Include "FWMBrowse.ch"

User Function M3MVC()

Local cAlias  := "SC5"
Local cTitle  := "Pedidos de Venda"
Local oBrowse := FWMBrowse():New()

Private aRotina := MenuDef()

oBrowse:SetAlias(cAlias)
oBrowse:SetDescription(cTitle)

oBrowse:Activate()

Return NIL

// ------------------------------------------------------------------------------------------ //

Static Function MenuDef()

Local aRotina := {}

ADD OPTION aRotina TITLE "Pesquisar"  ACTION "PESQBRW"        OPERATION 1 ACCESS 0
ADD OPTION aRotina TITLE "Visualizar" ACTION "VIEWDEF.M3_MVC" OPERATION 2 ACCESS 0
ADD OPTION aRotina TITLE "Incluir"    ACTION "VIEWDEF.M3_MVC" OPERATION 3 ACCESS 0
ADD OPTION aRotina TITLE "Alterar"    ACTION "VIEWDEF.M3_MVC" OPERATION 4 ACCESS 0
ADD OPTION aRotina TITLE "Excluir"    ACTION "VIEWDEF.M3_MVC" OPERATION 5 ACCESS 0
ADD OPTION aRotina TITLE "Imprimir"   ACTION "VIEWDEF.M3_MVC" OPERATION 8 ACCESS 0
ADD OPTION aRotina TITLE "Copiar"     ACTION "VIEWDEF.M3_MVC" OPERATION 9 ACCESS 0

Return aRotina

// ------------------------------------------------------------------------------------------ //

Static Function ModelDef()

Local oStruSC5 := FWFormStruct(1, "SC5")
Local oStruSC6 := FWFormStruct(1, "SC6")
Local oModel   := MPFormModel():New("M3M")

oModel:AddFields("SC5MASTER", /*cOwner*/, oStruSC5)
oModel:AddGrid("SC6DETAIL", "SC5MASTER", oStruSC6)
oModel:SetRelation("SC6DETAIL", {{'C6_FILIAL', 'xFilial("SC6")'}, {"C6_NUM", "C5_NUM"}}, SC6->(IndexKey(1)))
oModel:SetDescription("Pedidos de Venda")
oModel:GetModel("SC5MASTER"):SetDescription("Pedido")
oModel:GetModel("SC6DETAIL"):SetDescription("Itens")

Return oModel

// ------------------------------------------------------------------------------------------ //

Static Function ViewDef()

Local oStruSC5 := FWFormStruct(2, "SC5")
Local oStruSC6 := FWFormStruct(2, "SC6")
Local oModel   := FWLoadModel("M3_MVC")           	
Local oView    := NIL

oView := FWFormView():New()

oView:SetModel(oModel)
oView:AddField("VIEW_SC5", oStruSC5, "SC5MASTER")
oView:AddGrid("VIEW_SC6", oStruSC6, "SC6DETAIL")
oView:CreateHorizontalBox("SUPERIOR", 50)
oView:CreateHorizontalBox("INFERIOR", 50)
oView:SetOwnerView("VIEW_SC5", "SUPERIOR")
oView:SetOwnerView("VIEW_SC6", "INFERIOR")
oView:EnableTitleView("VIEW_SC5")
oView:EnableTitleView("VIEW_SC6")

Return oView